<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">


        <a-row>
          <a-col :xl="12" :lg="12" :md="12" :sm="24" :xs="24">
            <a-form-model-item label="班级名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="gradeCode">
              <j-dict-select-tag type="list" v-model="model.gradeCode" dictCode="" placeholder="年级"
                                 dictCode="cou_grade,grade_name,id" style="width: 50%"/>
              <j-dict-select-tag type="list" v-model="model.classNumber" dictCode="" placeholder="班级名称"
                                 dictCode="class_number"
                                 @change="changeClassGradesessionYear"
                                 style="width: 50%" />
            </a-form-model-item>
<!--            <a-form-model-item label="班级名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="classNumber">-->
<!--              &lt;!&ndash;              <a-input-number v-model="model.classNumber" placeholder="请输入班号" style="width: 50%" />&ndash;&gt;-->
<!--              -->
<!--            </a-form-model-item>-->
          </a-col>
          <a-col :xl="12" :lg="12" :md="12" :sm="24" :xs="24">
            <a-form-model-item label="简称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="classNickname">
              <a-input v-model="model.classNickname" placeholder="请输入班级编码" ></a-input>
            </a-form-model-item>
            <!--            <a-form-model-item label="班级名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="classNumber">-->
            <!--              &lt;!&ndash;              <a-input-number v-model="model.classNumber" placeholder="请输入班号" style="width: 50%" />&ndash;&gt;-->
            <!--              -->
            <!--            </a-form-model-item>-->
          </a-col>

        </a-row>

        <a-row >
          <a-col :xl="12" :lg="12" :md="12" :sm="24" :xs="24">
            <a-form-model-item label="入学时间" :labelCol="labelCol" :wrapperCol="wrapperCol"  prop="classEnrollmentDate">
              <j-date v-model="model.classEnrollmentDate" :showTime="false" dateFormat="YYYY-MM-DD" />
<!--              <label style="width: 500px">届级:</label>-->
<!--              <j-dict-select-tag label="届级"  type="list" v-model="model.classGradesessionYear" dictCode="" placeholder="请选择届级"-->
<!--                                 dictCode="cou_gradesession_year,gradesession_year_name,gradesession_year_code,iz_open=1 and del_flag=0 order by gradesession_year_code desc" style="width: 50%" />-->

            </a-form-model-item>
          </a-col>
          <a-col  :xl="12" :lg="12" :md="12" :sm="24" :xs="24" >
              <a-form-model-item label="届级" :labelCol="labelCol" :wrapperCol="wrapperCol"  prop="classGradesessionYear">
                <j-dict-select-tag label="届级" @change="changeClassGradesessionYear" type="list" v-model="model.classGradesessionYear" dictCode="" placeholder="请选择届级"
                                   dictCode="cou_gradesession_year,gradesession_year_name,gradesession_year_code,iz_open=1 and del_flag=0 order by gradesession_year_code desc" />

              </a-form-model-item>

           </a-col>
        </a-row>

        <a-row >

          <a-col :xl="12" :lg="12" :md="12" :sm="24" :xs="24">
            <a-form-model-item label="班级编码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="classCode">
              <a-input v-model="model.classCode" placeholder="请输入班级编码" ></a-input>
            </a-form-model-item>
          </a-col>
<!--          <a-col :xl="12" :lg="12" :md="12" :sm="24" :xs="24">-->
<!--            <a-form-model-item label="班级名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="className">-->
<!--              <a-input v-model="model.className" placeholder="请输入班级名称"></a-input>-->
<!--            </a-form-model-item>-->
<!--          </a-col>-->
<!--          <a-col :xl="12" :lg="12" :md="12" :sm="24" :xs="24">-->
<!--            <a-form-model-item label="班级简称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="classNickname">-->
<!--              <a-input v-model="model.classNickname" placeholder="请输入班级简称"></a-input>-->
<!--            </a-form-model-item>-->
<!--          </a-col>-->
          <a-col :xl="12" :lg="12" :md="12" :sm="24" :xs="24">
            <a-form-model-item label="班级类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="classTtype">
              <j-dict-select-tag type="list" v-model="model.classTtype" dictCode="class_ttype" placeholder="请选择班级类型" />
            </a-form-model-item>
          </a-col>

        </a-row>

        <a-row >

          <a-col :xl="12" :lg="12" :md="12" :sm="24" :xs="24">
            <a-form-model-item label="班级容量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="classStudentCapacity">
              <a-input-number v-model="model.classStudentCapacity" placeholder="请输入班级容量" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :xl="12" :lg="12" :md="12" :sm="24" :xs="24">
            <a-form-model-item label="班级状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="classStatus">
              <j-dict-select-tag type="list" v-model="model.classStatus" dictCode="class_status"
                                 placeholder="请选择班级状态" />
            </a-form-model-item>
          </a-col>

        </a-row>

        <a-row >
          <a-col :xl="12" :lg="12" :md="12" :sm="24" :xs="24">
            <a-form-model-item label="班主任" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="classHeadmasterCode">
<!--              <j-dict-select-tag type="list" v-model="model.classHeadmasterCode" dictCode="tea_teacher as t,t.teacher_name,t.id,t.teacher_iz_open =1 and t.teacher_iz_delete  =0 order by t.teacher_order !! t.teacher_name"-->
<!--                                 placeholder="请选择班主任" />-->
              <j-select-multi-teacher v-model='model.classHeadmasterCode'></j-select-multi-teacher>
            </a-form-model-item>
          </a-col>
          <a-col :xl="12" :lg="12" :md="12" :sm="24" :xs="24">
            <a-form-model-item label="班级人数" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="classStudentCount">
              <a-input-number v-model="model.classStudentCount" placeholder="请输入班级人数" style="width: 100%" />
            </a-form-model-item>
          </a-col>

        </a-row>

        <a-row >
          <a-col :xl="12" :lg="12" :md="12" :sm="24" :xs="24">
            <a-form-model-item label="班级排序" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="classOrder">
              <a-input-number v-model="model.classOrder" placeholder="请输入班级排序" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :xl="12" :lg="12" :md="12" :sm="24" :xs="24">
            <a-form-model-item label="班级备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="classRemark">
              <a-input v-model="model.classRemark" placeholder="请输入班级备注"></a-input>
            </a-form-model-item>
          </a-col>

        </a-row>

        <a-row >
          <a-col :xl="12" :lg="12" :md="12" :sm="24" :xs="24">
            <a-form-model-item label="是否启用" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="classIzOpen">
              <j-dict-select-tag type="radio" v-model="model.classIzOpen" dictCode="iz_open" placeholder="请选择是否启用" />
            </a-form-model-item>
          </a-col>

        </a-row>

      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

import { httpAction, getAction } from '@api/manage'
import { validateDuplicateValue } from '@/utils/util'
import JSelectMultiTeacher from '@views/smart_school/components/public/JSelectMultiTeacher'

export default {
  name: 'CouClassForm',
  components: { JSelectMultiTeacher },
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      model: {
        classIzOpen: 1,
        classStatus: "1",
        classTtype: "5",
        classStudentCapacity: 50,
        classEnrollmentDate:  new Date().getFullYear()+'-09-01',
        classGradesessionYear: new Date().getFullYear(),
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      validatorRules: {
        // classCode: [
        //   { required: true, message: '请输入班级编码!' }
        // ],
        gradeCode: [
          { required: true, message: '请选择年级!' }
        ],
        classNumber: [
          { required: true, message: '请选择编辑名称(班号)!' }
        ],
        classStatus: [
          { required: true, message: '请输入班级状态!' }
        ],
        classIzOpen: [
          { required: true, message: '请输入是否启用!' }
        ]
      },
      url: {
        add: '/classes/couClass/add',
        edit: '/classes/couClass/edit',
        queryById: '/classes/couClass/queryById'
      }
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    }
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    add() {
      this.edit(this.modelDefault)
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.visible = true
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message)
              that.$emit('ok')
            } else {
              that.$message.warning(res.message)
            }
          }).finally(() => {
            that.confirmLoading = false
          })
        }

      })
    },
    changeClassGradesessionYear(){
      if(this.model.classGradesessionYear && this.model.classNumber && !this.model.classCode){
        this.model.classCode = this.model.classGradesessionYear+this.model.classNumber;
      }
    }
  }
}
</script>